#pragma once

#include <iostream>
#include <cstdio>
#include <cerrno>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
const std::string comm_path = "./myfifo";

#define Creater 1
#define User    2
#define DefaultFD -1
#define Read  O_RDONLY
#define Write O_WRONLY
#define BadeSize 4096
class NamePiped
{
private:
    bool OpenNamedPipe(int mode)
        {
            _fd = open(_fifo_path.c_str(),mode);
            if(_fd<0) 
            return false;
            return true;
        }
public:
    NamePiped(const std::string &path,int who)
     : _fifo_path(path),_id(who),_fd(DefaultFD)
    {

        if(_id == Creater)
        {
            int res=mkfifo(path.c_str(),0666);
        if(res!=0)
        {
             perror("mkfifo");
        }
        std::cout<<"creater create named pipe"<<std::endl;
        }
    }

    bool OpenForRead()
    {
        return OpenNamedPipe(Read);
    }

    
    bool OpenForWrite()
    {
        return OpenNamedPipe(Write);
    }

    int ReadNamedPipe(std::string *out)
    {
        char buffer[BadeSize];
        int n = read(_fd,buffer,sizeof(buffer));
        if(n>0)
        {
            buffer[n] = 0;
            *out = buffer;
        }
        return n;
    }

    int WriteNamedPipe(const std::string &in)
    {
        return write(_fd,in.c_str(),in.size());
    }

    ~NamePiped()
    {
        if(_id==Creater)
        {
        sleep(3);
        int res=unlink(_fifo_path.c_str());
        if(res!=0)
        {
            perror("unlink");
        }
        std::cout<<"creater create named pipe"<<std::endl;
        }
        if(_fd != DefaultFD)
        close(_fd);
    }

private:
    const std::string _fifo_path;
    int _id;
    int _fd;
};